# -*- coding:utf-8 -*-
# @Author:Cheng Lei 1037654919@qq.com
# @Time : 2023/3/27 下午5:01
# @FileName: kuwo_t4_singer.py
# @Software: PyCharm
import math

# 第四 新增歌手
import requests
import time
import random
from utils import MongoDBUtil,get_html,mongo_manager
from multiprocessing import Pool
from class_kuwo_test import Kuwo
MongoDBUtil = MongoDBUtil()
playlist_tag = mongo_manager("playlist_tag", db="kuwo")
playlist_song= mongo_manager("playlist_song", db="kuwo")
playlist_song_list= mongo_manager("playlist_song_list", db="kuwo")
singer_list = mongo_manager("singer_list", db="kuwo")

def get_proxy():
    port = random.randint(24000, 24400)
    proxies = {'http': f'http://zheng123:zheng123@haproxy.iinti.cn:{port}',
               'https': f'http://zheng123:zheng123@haproxy.iinti.cn:{port}'}
    return proxies


kuwo = Kuwo()

# 更新歌曲列表
def playlist_song_list_run():
    seeds = playlist_song.findAll({"status": None})
    for seed in seeds:
        result ={"_id":seed['rid']}
        try:
            # print(data)
            MongoDBUtil.insert_one('playlist_song_list', result)
        except Exception as e:
            print(e)
        seed['status']='success'
        playlist_song.updateOne({'_id': seed['_id']}, seed)


# 插入数据
def insert_data(tablename, datas):
    if len(datas) > 0 and datas is not None:
        for data in datas:
            # data['_id'] = data['id']
            try:
                # print(data)
                MongoDBUtil.insert_one(tablename, data)
            except Exception as e:
                print(e)
    else:
        pass


# 解析歌曲信息
def parse_song(data):
    datass = data['artistList']
    results=[]
    for datas in datass:
        result = {'_id': datas.get("id"), 'id': datas.get("id"),
              'name': datas.get("name"), 'artistFans': datas.get("artistFans"),
              'albumNum': datas.get("albumNum"), 'mvNum': datas.get("mvNum"),
              'pic': datas.get("pic"), 'musicNum': datas.get("musicNum"),
              'isStar': datas.get("isStar"),'content_type': datas.get("content_type"),
              'aartist': datas.get("aartist")
              }
        results.append(result)
    return results


# 获取歌曲信息
def get_artist_info(category):
    data2=kuwo.get_artist_lsit(category=category)
    pages=math.ceil(int(data2['total'])/60)
    for page in range(1,pages+1):
        data2 = kuwo.get_artist_lsit(category=category, pn=page)
        results = parse_song(data2)
        insert_data(tablename='singer_list', datas=results)
    print('{} Done'.format(category))



# 获取歌曲信息 run
def get_artist_info_run():
    categorys = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
                 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '#']

    pool = Pool(processes=32)
    pool.map(get_artist_info, categorys)
    pool.close()  # 关闭进程池，不再接受新的进程
    pool.join()


if __name__ == '__main__':

    kuwo = Kuwo()
    # pn = 1
    # category = 'C'
    # data2=kuwo.get_artist_lsit(category=category, pn=pn)
    # results = parse_song(data2)
    # print(results)
    # insert_data(tablename='singer_list', datas=results)
    get_artist_info_run()
    pass

